<template>
  <div class="upload-container">
    <template v-if="imageUrl">
      <div class="image-preview" v-if="isImage(imageUrl)">
        <div class="image-preview-wrapper">
          <img :src="$S.path + imageUrl">
          <div class="image-preview-action">
            <i @click="rmImage" class="el-icon-delete"></i>
          </div>
        </div>
      </div>
      <div class="file-preview" v-else>
        <el-tag closable @close="rmImage">{{imageUrl}}</el-tag>
      </div>
    </template>
    <el-upload
      v-if="!imageUrl"
      v-loading="pload"
      :headers="{accessToken:$store.state.user.token}"
      class="image-uploader"
      :data="dataObj"
      drag
      :multiple="false"
      :show-file-list="false"
      action="/api1/file/commonUpload"
      :on-success="handleScucess"
      :on-error="handleError"
      :before-upload="beforeUpload"
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
    </el-upload>
  </div>
</template>

<script>
  import { upload } from '@/utils/weApi'
  export default {
    name: 'singleImageUpload',
    props: {
      value: String
    },
    computed: {
      imageUrl() {
        return this.value
      }
    },
    data() {
      return {
        tempUrl: '',
        dataObj: {
          type: 1,
          path:'ABC'
        },
        pload: false
      }
    },
    methods: {
      rmImage() {
        this.emitInput('')
      },
      emitInput(val) {
        this.$emit('input', val)
      },
      handleScucess(c) {
        this.emitInput(c.path)
        this.pload = false
        this.$notify({title: '上传成功', type: 'success', duration: 3000})
      },
      handleError(c) {
        this.pload = false
        this.$notify({title: JSON.parse(c.message).message, type: 'error', duration: 3000})
      },
      beforeUpload(file) {
        if(!this.isDoc(file.name)) {
          this.$message.error('请上传html文件')
          return false
        }
        this.pload = true
      },
      isImage (str) {
        return str.match(/(^data:image\/.*,)|(\.(jp(e|g|eg)|gif|png|bmp|webp)((\?|#).*)?$)/i);
      },
      isDoc (str) {
        return str.match(/(\.(html)((\?|#).*)?$)/i);
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .upload-container {
    display: flex;
    .image-uploader {
      max-width: 300px;
      height: 175px;
      flex: 1;
    }
    .image-preview {
      height: 175px;
      width: 175px;
      position: relative;
      border: 1px dashed #d9d9d9;
      .image-preview-wrapper {
        position: relative;
        width: 100%;
        height: 100%;
        img {
          max-width: 100%;
          max-height: 100%;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
        }
      }
      .image-preview-action {
        position: absolute;
        width: 100%;
        height: 100%;
        left: 0;
        top: 0;
        cursor: default;
        text-align: center;
        color: #fff;
        opacity: 0;
        font-size: 20px;
        background-color: rgba(0, 0, 0, .5);
        transition: opacity .3s;
        cursor: pointer;
        text-align: center;
        line-height: 200px;
        .el-icon-delete {
          font-size: 36px;
        }
      }
      &:hover {
        .image-preview-action {
          opacity: 1;
        }
      }
    }
  }
</style>